var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread


function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}

function setFocusDelayed()
{
	global_valfield.focus();
}

function setFocus(valfield)
{
	global_valfield = valfield;
	setTimeout( 'setFocusDelayed()', 100 );
}


function showMessage(pFieldId, pMsgType, pMsg)
{
	var myMessage;
	if (emptyString.test(pMsg)) 
		myMessage = String.fromCharCode(nbsp);    
	else  
		myMessage = pMsg;

	var myElement = document.getElementById(pFieldId);
	myElement.firstChild.nodeValue = myMessage;
	myElement.className = pMsgType;   // set the CSS class to adjust appearance of message
}

var proceed = 2;

function fieldChecker(pField, pFieldId, pIsRequired)
{
	if (!document.getElementById) 
		return true;  // not available on this browser - leave validation to the server
	var myElement = document.getElementById(pFieldId);
	if (!myElement.firstChild) return true;  // not available on this browser 
	if (myElement.firstChild.nodeType != node_text) return true;  // pField is wrong type of node  

	if (emptyString.test(pField.value)) {
		if (pIsRequired) {
			showMessage(pFieldId, "error", "Błąd: pole wymagane");  
			setFocus(pField);
			return false;
		} else {
			showMessage(pFieldId, "warn", "");   // OK
			return true;
		}
	} else {
		showMessage(pFieldId, "", "");
	}
  return proceed;
}

function validatePresent(pField, pFieldId)
{
	var myReturn = fieldChecker (pField, pFieldId, true);
	return myReturn;
}

function validateEmail(pField, pFieldId, pIsRequired)
{
	var myReturn = fieldChecker (pField, pFieldId, pIsRequired);
	if (myReturn != proceed) return myReturn;

	var tfld = trim(pField.value);  // value of field with whitespace trimmed off
	var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
	if (!email.test(tfld)) {
		showMessage(pFieldId, "error", "Błąd: Nie poprawny adres.");
		setFocus(pField);
		return false;
	}

	var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
	if (!email2.test(tfld)) 
		showMessage(pFieldId, "warn", "Sprawdz poprawnosc adresu.");
	else
		showMessage(pFieldId, "warn", "");
	return true;
}

function validatePassword(pField1, pField2, pField1ById, pField2ById)
{
	var myReturn = false;
	var myPasswd1 = pField1.value;
	var myPasswd2 = pField2.value;
	var myElement1 = document.getElementById(pField1ById);
	var myElement2 = document.getElementById(pField2ById);
	
	
	if (myPasswd1 == "" || myPasswd2 == ""){
		myElement1.firstChild.nodeValue = "Hasło nie może być puste";
		myElement2.firstChild.nodeValue = "Hasło nie może być puste";
	} else if (myPasswd1 != myPasswd2){
		myElement1.firstChild.nodeValue = "Hasła są różne";		
		myElement2.firstChild.nodeValue = "Hasła są różne";
	} else if (myPasswd1.length < 5 || myPasswd2.length < 5) {
		myElement1.firstChild.nodeValue = "Hasło jest za krótkie";		
		myElement2.firstChild.nodeValue = "Hasło jest za krótkie";
	} else {
		myElement1.firstChild.nodeValue = "Hasło jest OK.";
		myElement2.firstChild.nodeValue = "Hasło jest OK.";
		myReturn = true;
	}
	return myReturn;
}

